package com.blockmeta.bbs.immodule.activity;

import android.content.Intent;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.View;

import com.blockmeta.bbs.businesslibrary.base.BaseActivity;
import com.blockmeta.bbs.businesslibrary.net.pojo.BaseCodeResponse;
import com.blockmeta.bbs.businesslibrary.net.retrofit.RetrofitFactory;
import com.blockmeta.bbs.businesslibrary.net.rxjava.CommonSubscriber;
import com.blockmeta.bbs.businesslibrary.net.rxjava.RxUtil;
import com.blockmeta.bbs.immodule.R;
import com.blockmeta.bbs.immodule.R2;
import com.blockmeta.bbs.immodule.activity.chat.CustomerServiceChatActivity;
import com.blockmeta.bbs.immodule.adapter.CustomerServiceAdapter;
import com.blockmeta.bbs.immodule.bean.Params;
import com.blockmeta.bbs.immodule.bean.databean.UserBean;
import com.blockmeta.bbs.immodule.corebean.Impl.IMApplicationContext;
import com.blockmeta.bbs.immodule.net.api.GetServiceUserListApi;
import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;


/**
 * 客服 服务号页面
 */
public class CustomerServiceActivity extends BaseActivity implements BaseQuickAdapter.OnItemClickListener {

    @BindView(R2.id.rcv_fans_list)
    RecyclerView mRcvList;

    private List<UserBean> mListData = new ArrayList<>();
    private CustomerServiceAdapter mAdapter;
    private long mUserId;

    @Override
    protected boolean showTitle() {
        return true;
    }

    @Override
    public View setView() {
        return getLayoutInflater().inflate(R.layout.activity_customer_service,null);
    }

    @Override
    protected void initialize() {
        super.initialize();
        mUserId = getIntent().getLongExtra(Params.USER_TAG, 0);
        setTitleText("服务号");
        initView();
        network();
    }

    private void network() {
        RetrofitFactory.getRetrofit().create(GetServiceUserListApi.class)
                .getServiceUserListApi()
                .compose(this.<BaseCodeResponse<List<UserBean>>>bindToLifecycle())
                .compose(RxUtil.<List<UserBean>>commonResultByCode())
                .compose(RxUtil.<List<UserBean>>rxSchedulerHelper())
                .subscribe(new CommonSubscriber<List<UserBean>>(mContext) {
                    @Override
                    public void onNext(List<UserBean> list) {
                        mListData.addAll(list);
                        mAdapter.notifyDataSetChanged();
                        IMApplicationContext.getUserDomainService().addCustomerServiceUsers(list);
                    }
                });
    }

    private void initView() {
        mRcvList.setLayoutManager(new LinearLayoutManager(mContext));
        mAdapter = new CustomerServiceAdapter(R.layout.item_customer_service, mListData);
        mAdapter.bindToRecyclerView(mRcvList);
        mAdapter.setOnItemClickListener(this);
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        UserBean item = mListData.get(position);

        startActivity(new Intent(CustomerServiceActivity.this, CustomerServiceChatActivity.class)
                .putExtra(Params.USER_TAG, item.getId()));
//        RouterActivityHelper.openUserCenter(mContext, String.valueOf(item.getFansId()));
    }
}
